package com.rui.study.micro.gateway.controller;

import com.netflix.discovery.shared.Application;
import com.netflix.eureka.EurekaServerContext;
import com.netflix.eureka.EurekaServerContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: yaowr
 * @create: 2019-04-16 09:36
 */
@Slf4j
@RestController
@RequestMapping("/api/svr")
public class CustomEurekaController {

    @Autowired
    EurekaServerContext context;

    @GetMapping("list")
    public ResponseEntity findServiceList() {
        EurekaServerContext context = EurekaServerContextHolder.getInstance().getServerContext();

        List<Application> list = context.getRegistry().getSortedApplications();

        return new ResponseEntity(list, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity findServiceByAppName(@RequestBody String appName) {
        if (appName == null || appName.length() == 0) {
            return null;
        }
        String capitalAppName = appName.toUpperCase();
        log.info("capitalAppName is: {}", capitalAppName);
        Application application = this.context.getRegistry().getApplication(capitalAppName);
        return new ResponseEntity(application, HttpStatus.OK);
    }
}
